{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e5be2aa-dc22-4ba0-ba93-8c98bdc27265",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 7.红外遥控器控制\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：7.红外遥控器控制.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：红外遥控器控制\n",
    "红外遥控器控制程序，通过pylirc库接收红外遥控器信号，把接收到的红外遥控器控制信号解码后，判断哪个键按下，来控制机器人运动！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b635aca-632c-4543-8b28-735367190e7e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c444c32-96b5-4946-bb1a-8178693fea01",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from LOBOROBOT import LOBOROBOT  # 载入机器人库\n",
    "import pylirc as lirc\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3082b4b0-71f7-4d6f-b331-352df233ba69",
   "metadata": {},
   "source": [
    "## 2. 定义机器人对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78c5e70c-4a38-4d8a-8a89-4da5280b904d",
   "metadata": {},
   "outputs": [],
   "source": [
    "clbrobot = LOBOROBOT() # 实例化机器人对象\n",
    "makerobo_blocking = 0 # 判断值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a40a5c5-ae17-4b2e-a3dc-2496ddb115c2",
   "metadata": {},
   "source": [
    "## 3.初始化函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce3e3da4-3cc1-4c3c-98cd-120a7b548acc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GPIO初始化设置\n",
    "def makerobo_setup():\n",
    "\tlirc.init(\"pylirc\", \"/etc/lirc/conf\", makerobo_blocking) # 载入配置参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d4ad178-6e4f-44cf-b498-b5b1c8e9eb11",
   "metadata": {},
   "source": [
    "## 4 .红外遥控控制函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bda7fd2-2b2f-4d71-95a3-b2dfc8547e8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def robot_control(config):\n",
    "    global color\n",
    "    # 按键第一行第一个\n",
    "    if config == 'KEY_CHANNELDOWN':\n",
    "        clbrobot.forward_Left(50,3) # 机器人前左斜\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot front left oblique')\n",
    "    if config == 'KEY_CHANNEL':     # 按键第一行第二个\n",
    "        clbrobot.t_up(50,3)         # 机器人前进\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot advance')\n",
    "    if config == 'KEY_CHANNELUP':     # 按键第一行第三个\n",
    "        clbrobot.forward_Right(50,3)  # 机器人前右斜\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot front right tilt')\n",
    "    if config == 'KEY_PREVIOUS':    # 第二行第一个\n",
    "        clbrobot.moveLeft(50,3)\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot shift left')\n",
    "    if config == 'KEY_NEXT':       # 第二行第二个\n",
    "        clbrobot.t_stop(1)         # 机器人停止\n",
    "        print ('Robot stop')\n",
    "    if config == 'KEY_PLAYPAUSE':  # 第二行第三个\n",
    "        clbrobot.moveRight(50,3)   # 机器人右移\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot right shift')\n",
    "    if config == 'KEY_VOLUMEDOWN': # 第三行第一个\n",
    "        clbrobot.backward_Left(50,3)   # 机器人后左斜\n",
    "        clbrobot.t_stop(1)             # 停止\n",
    "        print ('Robot back left oblique')\n",
    "    if config == 'KEY_VOLUMEUP':  # 第三行第二个\n",
    "        clbrobot.t_down(50,3)    # 机器人后退\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot retreat')\n",
    "    if config == 'KEY_EQUAL':    # 第三行第三个\n",
    "        clbrobot.backward_Right(50,3) # 机器人后右斜\n",
    "        clbrobot.t_stop(1)          # 停止\n",
    "        print ('Robot back right oblique')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7357f3de-95cd-4994-9616-3450f7c113a2",
   "metadata": {},
   "source": [
    "## 7.循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "252547e4-ea35-4871-ae35-8aaa969c1c3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "\twhile True:\n",
    "\t\ts = lirc.nextcode(1)    # 获取红外遥控器码值\t\t\n",
    "\t\twhile(s):\n",
    "\t\t\tfor (code) in s:\n",
    "\t\t\t\tprint (\"Command: \", code[\"config\"]) # 调试信息，可以具体知道按下了哪个按键\n",
    "\t\t\t\trobot_control(code[\"config\"])  # 调用控制RGB函数\n",
    "\t\t\tif(not makerobo_blocking):       # 读取到值\n",
    "\t\t\t\ts = lirc.nextcode(1)       # 再一次获取红外遥控器码值\t\n",
    "\t\t\telse:\n",
    "\t\t\t\ts = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c0a12ac-0d80-4227-805d-6e38e73257dc",
   "metadata": {},
   "source": [
    "## 8.释放函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "319509cb-8d4e-4ec8-bdee-ac49b3cff4a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def destroy():\n",
    "    led.close()\n",
    "    lirc.exit()  # 退出红外遥控器接收"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6ca72ea-460d-4b40-a0ad-0842ec4177a4",
   "metadata": {},
   "source": [
    "## 9.程序入口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94c4e730-8686-46db-8e13-8ecfb95cfac3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "\ttry:\n",
    "\t\tmakerobo_setup() # GPIO初始化程序\n",
    "\t\tmakerobo_loop()  # 调用循环函数\n",
    "\texcept KeyboardInterrupt: # 如果按下ctrl + C,退出，处理异常\n",
    "\t\tdestroy() # 释放资源"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
